package arr

// 将array中的所有元素转换为由sep分隔的字符串

import (
	"gitee.com/big_meteor/godash/lang"
	"strings"
)

func join(elems []string, sep string) string {
	n := len(sep) * (len(elems) - 1)
	for i := 0; i < len(elems); i++ {
		n += len(elems[i])
	}

	var b strings.Builder
	b.Grow(n)
	b.WriteString(elems[0])
	for _, s := range elems[1:] {
		b.WriteString(sep)
		b.WriteString(s)
	}
	return b.String()
}

func Join(elems []string, sep string) string {
	switch len(elems) {
	case 0:
		return ""
	case 1:
		return elems[0]
	}

	return join(elems, sep)
}

func JoinFloat(elems []float64, sep string) string {
	switch len(elems) {
	case 0:
		return ""
	case 1:
		return lang.ToStringFloat(elems[0])
	}

	sa := make([]string, len(elems))
	for i := 0; i < len(elems); i++ {
		sa[i] = lang.ToStringFloat(elems[i])
	}

	return join(sa, sep)
}

func JoinInt(elems []int, sep string) string {
	switch len(elems) {
	case 0:
		return ""
	case 1:
		return lang.ToString(elems[0])
	}

	sa := make([]string, len(elems))
	for i := 0; i < len(elems); i++ {
		sa[i] = lang.ToString(elems[i])
	}

	return join(sa, sep)
}

func JoinInt64(elems []int64, sep string) string {
	switch len(elems) {
	case 0:
		return ""
	case 1:
		return lang.ToStringInt64(elems[0])
	}

	sa := make([]string, len(elems))
	for i := 0; i < len(elems); i++ {
		sa[i] = lang.ToStringInt64(elems[i])
	}

	return join(sa, sep)
}

func JoinUInt(elems []uint, sep string) string {
	switch len(elems) {
	case 0:
		return ""
	case 1:
		return lang.ToStringUInt(elems[0])
	}

	sa := make([]string, len(elems))
	for i := 0; i < len(elems); i++ {
		sa[i] = lang.ToStringUInt(elems[i])
	}

	return join(sa, sep)
}

func JoinUInt64(elems []uint64, sep string) string {
	switch len(elems) {
	case 0:
		return ""
	case 1:
		return lang.ToStringUInt64(elems[0])
	}

	sa := make([]string, len(elems))
	for i := 0; i < len(elems); i++ {
		sa[i] = lang.ToStringUInt64(elems[i])
	}

	return join(sa, sep)
}
